<?php
class Login extends Controller {
	private $data;
	private $username_data;
	function index() {
		$this->loadData();
		$this->load->helper('form');
		$this->load->view('loginview',$this->data);
		//		form_open('mainpage');
		//		form_input($this -> data['username_data']);
		//		form_close();
	}

	private function loadData() {
		$this -> data['title'] = 'Login';
		$this -> data['message'] = 'Please login';
		$this -> data['username_str'] = 'Username:';
		$this -> data['password_str'] = 'Password:';

		$this -> data['username_data'] = array(
              'name'        => 'username',              
              'value'       => '',
              'maxlength'   => '32',
              'size'        => '50',
              );
        $this -> data['password_data'] = array (
			'name' => 'password',
			'size' => '50'
		);
	}
	
	function verifyUser() {
		$username = $_POST['username'];
		$password = $_POST['password'];
		$this -> load -> model('Users');
		$this -> load -> library('session');
		
		if ( $this -> Users -> correctPassword($username,$password)) {			
			$role = $this -> Users -> getRoles($username);
			$this -> session -> set_userdata('username',$username);
			$this -> session -> set_userdata('role',$role);
			switch ($role) {
				case 'GD':
					redirect('FirstPage/Giamdoc');
					break;
				case 'QL':
					redirect('FirstPage/Quanly');
					break;
				case 'NV':
					redirect('FirstPage/Nhanvien');
					break;
				default: die('Error: User '.$username.' has no role in the company !');
			}
		}
		
	}
}
?>